package com.madou.dbtool.redis.panel.ui.types;

import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.redis.panel.dataloader.RedisHashTableDataLoader;
import com.madou.dbtool.ui.table.MColumn;
import com.madou.dbtool.ui.table.MTable;
import com.madou.dbtool.ui.table.loader.TableDataLoader;
import com.madou.inters.util.TipUtils;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 获取查询器内容
 * @author miukoo
 */
@HideClass
public class OperatorPanel extends JPanel implements TableModelListener {
    protected MTable table;
    private JToolBar pageBar;
    private JButton addButton;
    private JButton deleteButton;
    private JButton applayButton;
    private JButton refreshButton;
    String selectKey;
    RedisSourceManager redisSourceManager;
    public OperatorPanel(RedisSourceManager redisSourceManager, TableDataLoader tableDataLoader, String selectKey, List<MColumn> columns) throws Exception {
        this.selectKey = selectKey;
        this.redisSourceManager = redisSourceManager;
        this.setLayout(new BorderLayout());
        table = new MTable(false,columns,tableDataLoader);
        Color color = UIManager.getColor("EditorPane.background");
        if(color==null){
            color = Color.WHITE;
        }
        table.setBackground(color);
        table.load();
        JScrollPane jScrollPane = new JScrollPane(table);
        jScrollPane.getViewport().setBackground(color);
        jScrollPane.setBackground(color);
        this.add(jScrollPane, BorderLayout.CENTER);
        init();
    }

    public void init() {
        table.getModel().addTableModelListener(this);
        initPagePanel();
    }

    /**
     * 初始化分页按钮面板
     */
    private void initPagePanel(){
        addButton = new JButton(AllIcons.Table.ADD);
        deleteButton= new JButton(AllIcons.Table.DELETE);
        applayButton= new JButton(AllIcons.Table.APPLAY);
        applayButton.setVisible(false);
        refreshButton= new JButton(AllIcons.Table.REFRESH);

        pageBar = new JToolBar();
        pageBar.setFloatable(false);
        pageBar.setOpaque(false);
        pageBar.add(addButton);
        pageBar.add(deleteButton);
        pageBar.add(refreshButton);
        pageBar.add(applayButton);
        this.add(pageBar,BorderLayout.NORTH);

        addButton.setText("");
        deleteButton.setText("");
        applayButton.setText("");
        refreshButton.setText("");

        applayButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    table.applyChangedData();
                    table.load();
                    TipUtils.showInfoMessage(DBToolBundle.message("redis.operator.panel.tip.save.success"),"Tips");
                } catch (Exception e) {
                    e.printStackTrace();
                    TipUtils.showErrorDialog(DBToolBundle.message("redis.operator.panel.tip.save.fail")+e.getMessage(),"Tips");
                    // 字段、索引等列表需要刷新成功后的变更
                    tableChanged(null);
                }
            }
        });

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int index = table.getRowCount()-1;
                int[] selectedRows = table.getSelectedRows();
                if(selectedRows!=null&&selectedRows.length>0){
                    index = selectedRows[0];
                }
                table.newLine(index);
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                int[] selectedRows = table.getSelectedRows();
                if(selectedRows!=null&&selectedRows.length>0){
                    table.deleteRow(selectedRows);
                }
            }
        });

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                actionPage(table.getPageCurrent());
            }
        });

        refreshButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                loadData();
            }
        });
    }

    /**
     * 设定调整到第几页
     * @param page
     */
    private void actionPage(int page){
        if(page<1){
            page = 1;
        }else if(page>table.getAllPage()){
            page = table.getAllPage();
        }
        table.setPageCurrent(page);
        loadData();
    }
    /**
     * 重新加载第1页数据
     */
    public void reLoadData() {
        actionPage(1);
    }

    /**
     * 加载表格数据
     */
    private void loadData() {
        try {
            table.load();
        } catch (Exception e) {
        }
    }

    @Override
    public void tableChanged(TableModelEvent tableModelEvent) {
        int updatedRowCount = table.getUpdatedRowCount();
        if (updatedRowCount > 0) {
            applayButton.setText(String.format(DBToolBundle.message("redis.operator.panel.button.save"), updatedRowCount));
            applayButton.setVisible(true);
        } else {
            applayButton.setText("");
            applayButton.setVisible(false);
        }
    }


}
